﻿using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Xps.Packaging;
using ReflectionStudio.Classes;
namespace WPFExcelReport
{
    public partial class THELP
    {
        #region ==IsHelp===========
        public static readonly DependencyProperty IsHelpProperty = DependencyProperty.Register("IsHelp",
            typeof(bool), typeof(THELP),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsHelpChanged));
        public bool IsHelp
        {
            get
            {
                return (bool)GetValue(IsHelpProperty);
            }
            set
            {
                SetValue(IsHelpProperty, value); 
                OnHelp();
            }
        }
        private static void OnIsHelpChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as THELP;
            if (src != null)
            {
                src.IsHelp = (bool)e.NewValue;
            }
        }
        public void OnHelp()
        {
            if (!IsHelp)
                return;
            OnEXAUFChg(EXAUF);
        }
        #endregion

        public THELP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VHELP();
            DataContext = MV;
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
         } 

        public override void OnEXAUFChg(string vlaue)
        {
            if (!IsHelp)
                return;
            if (!IsLoaded)
                return;
            if (EXAUF.Length == 0)
                return;
            ART = EXAUF;
            bgWorker.RunWorkerAsync();
            base.OnEXAUFChg(vlaue);
        }
        public override void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Tracer.Verbose("HelpDocument:bw_DoWork", "START");
            try
            {

                string fileName = Path.Combine(PathHelper.ApplicationPath, @"Documents\"+app.company+"\\" + MV.ART.Trim() + ".xps");
                HelpDocumentDataContext ctx = new HelpDocumentDataContext() { FullName = fileName };
                e.Result = ctx.Load();

            }
            catch (Exception all)
            {
                Tracer.Error("HelpDocument.bw_DoWork", all);
            }
            finally
            {
                Tracer.Verbose("HelpDocument:bw_DoWork", "END");
            }
            base.bgWorker_DoWork(sender, e);
        }
        public override void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {


            // First, handle the case where an exception was thrown.
            if (e.Error != null)
            {
                Tracer.Error("HelpDocument.bw_RunWorkerCompleted", e.Error);
            }
            else
            {
                Tracer.Verbose("HelpDocument:bw_RunWorkerCompleted", "START");
                try
                {
                    if (e.Result != null && e.Result is XpsDocument)
                    {
                        XpsDocument xps = (XpsDocument)e.Result;
                        documentViewer1.Document = xps.GetFixedDocumentSequence();
                        Find("WD001");
                    }
                    WaitPanel.Stop();

                }
                catch (Exception all)
                {
                    Tracer.Error("HelpDocument.bw_RunWorkerCompleted", all);
                }
                finally
                {
                    Tracer.Verbose("HelpDocument:bw_RunWorkerCompleted", "END");
                }
            }
            base.bgWorker_RunWorkerCompleted(sender, e);
        }
        private void Find(string text = "")
        {
            object findToolbar = documentViewer1.Template.FindName("PART_FindToolBarHost", documentViewer1);
            if (findToolbar == null)
                return;
            if (text == "")
                return;
            try
            {
                FieldInfo findTextBoxFieldInfo = findToolbar.GetType().GetField("TextEditor", BindingFlags.NonPublic | BindingFlags.Instance);
                TextBox findTextBox = (TextBox)findTextBoxFieldInfo.GetValue(findToolbar);
                findTextBox.Text = text;
                FieldInfo findNextButtonFieldInfo = findToolbar.GetType().GetField("FindNextButton", BindingFlags.NonPublic | BindingFlags.Instance);
                Button findNextButton = (Button)findNextButtonFieldInfo.GetValue(findToolbar);
                findNextButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
            }
            catch (Exception ex)
            {
            }
        }
    }
}

